package com.huawei.esdk.tp.northcommu.cxf.common;

import java.util.List;
import org.apache.log4j.Logger;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.tp.business.common.sitemgr.SiteManagerService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.bean.DateTimeRange;
import com.huawei.esdk.tp.domain.model.bean.PagedList;
import com.huawei.esdk.tp.northcommu.cxf.common.convert.TPCommonSiteMgrConvert;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QueryAllSites;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QueryAllSitesResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteByName;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteByNameResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteByURI;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteByURIResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteFreeBusy;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteFreeBusyResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteStatus;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteStatusResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySitesFreeBusy;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySitesFreeBusyResponse;
import com.huawei.esdk.tp.northcommu.cxf.common.gen.TPCommonSiteMgr;

/**
 * 会场管理 cxf接口实现类 *
 * @author t00212088 * *
 */
public class TPCommonSiteMgrImpl implements TPCommonSiteMgr
{
    private static final Logger LOGGER = Logger
            .getLogger(TPCommonSiteMgrImpl.class);

    private SiteManagerService siteManager;

    private TPCommonSiteMgrConvert convert;

    /**
     * 通过名称查询会场 * @see
     * com.huawei.esdk.tp.northcommu.cxf.common.gen.TPMedicalSiteMgr#
     * querySiteByName
     * (com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteByName)
     */
    @Override
    public QuerySiteByNameResponse querySiteByName(QuerySiteByName parameters)
    {
        QuerySiteByNameResponse response = new QuerySiteByNameResponse();
        try
        {
            if(null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            SDKResult<List<Site>> result = siteManager
                    .querySiteByName(parameters.getSiteName());
            response = convert.sdkResult2QuerySiteByNameResponse(result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("", ex);
            response.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        return response;
    }

    /**
     * 
     * 查询单个会场忙闲状态
     */
    @Override
    public QuerySitesFreeBusyResponse querySitesFreeBusy(
            QuerySitesFreeBusy parameters)
    {
        QuerySitesFreeBusyResponse response = new QuerySitesFreeBusyResponse();
        try
        {
            if(null == parameters || null == parameters.getSiteUris())
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            SDKResult<List<Site>> result = siteManager.querySitesFreeBusy(
                    parameters.getSiteUris(), parameters.getTimeRange());

            response = convert.sdkResult2QuerySitesFreebusyResponse(
                    parameters.getSiteUris(), result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("", ex);
            response.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }

        return response;
    }

    /**
     * 通过会场uri查询会场信息 * @see
     * com.huawei.esdk.tp.northcommu.cxf.common.gen.TPMedicalSiteMgr#
     * querySiteByURI
     * (com.huawei.esdk.tp.northcommu.cxf.common.gen.QuerySiteByURI)
     */
    @Override
    public QuerySiteByURIResponse querySiteByURI(QuerySiteByURI parameters)
    {
        QuerySiteByURIResponse response = new QuerySiteByURIResponse();
        try
        {
            if(null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            SDKResult<Site> result = siteManager.querySiteByURI(parameters
                    .getSiteUri());
            response = convert.sdkResult2QuerySiteByURIResponse(result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("", ex);
            response.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        return response;
    }

    /**
     * 查询所有会场 * @see
     * com.huawei.esdk.tp.northcommu.cxf.common.gen.TPMedicalSiteMgr#
     * queryAllSites
     * (com.huawei.esdk.tp.northcommu.cxf.common.gen.QueryAllSites)
     */
    @Override
    public QueryAllSitesResponse queryAllSites(QueryAllSites parameters)
    {
        QueryAllSitesResponse response = new QueryAllSitesResponse();
        try 
        {
            if(null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            SDKResult<PagedList<Site>> result = siteManager.queryAllSites();
            response = convert
                .sdkResult2QueryAllSitesResponse(result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("", ex);
            response.setResultCode((int) ex.getSdkErrCode());
        }
        catch(Exception e)
        {
            LOGGER.error("",e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        return response;
    }

    /**
     * 查询多个会场忙闲状态
     */
    @Override
    public QuerySiteFreeBusyResponse querySiteFreeBusy(
            QuerySiteFreeBusy parameters)
    {
        QuerySiteFreeBusyResponse response = new QuerySiteFreeBusyResponse();
        try
        {
            if(null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            SDKResult<List<Site>> result = siteManager
                    .querySiteFreeBusy(parameters.getSiteUri());
            response = convert.querySiteFreebusyModel2Soap(
                    parameters.getSiteUri(), result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("", ex);
            response.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        return response;
    }

    public SiteManagerService getSiteManager()
    {
        return siteManager;
    }

    public void setSiteManager(SiteManagerService siteManager)
    {
        this.siteManager = siteManager;
    }

    public TPCommonSiteMgrConvert getConvert()
    {
        return convert;
    }

    public void setConvert(TPCommonSiteMgrConvert convert)
    {
        this.convert = convert;
    }


    public QuerySiteStatusResponse querySiteStatus(QuerySiteStatus parameters)
    {
        QuerySiteStatusResponse response = new QuerySiteStatusResponse();
        try
        {
            if (null == parameters || null == parameters.getSiteUris())
            {
                 response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                 return response;
            }
            DateTimeRange timeRange = convert
                    .querySiteStatusSoap2model(parameters.getBeginTime(),
                            parameters.getTimeRange());
            SDKResult<List<Site>> result = siteManager.querySiteStatus(
                    parameters.getSiteUris(), timeRange);

            response = convert
                    .querySiteStatusModel2soap(result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("", ex);
            response.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        return response;
    }
}
